package com.xzk.ExpressInnWeb.util;

import com.xzk.ExpressInnWeb.bean.BootstrapTableCourier;
import com.xzk.ExpressInnWeb.bean.Courier;
import com.xzk.ExpressInnWeb.bean.User;

import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CourierUtil {

    public static Courier buildCourier(ResultSet rs) {
        try {
            int id = rs.getInt("id");
            String couriername = rs.getString("couriername");
            String staffphone = rs.getString("staffphone");
            String personalid = rs.getString("personalid");
            String password = rs.getString("password");
            int delivery = rs.getInt("delivery");
            Timestamp createtime = rs.getTimestamp("createtime");
            Timestamp logintime = rs.getTimestamp("logintime");
            return new Courier(id,couriername,staffphone,personalid,password,delivery,createtime,logintime);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BootstrapTableCourier transferCourier(Courier courier){
        String createtime = DateFormatUtil.format(courier.getCreatetime());
        String logintime = courier.getLogintime()==null?"未曾登陆":DateFormatUtil.format(courier.getLogintime());
        String password = courier.getPassword().replaceAll("\\S","*");
        return new BootstrapTableCourier(
                courier.getId(),courier.getCouriername(),
                courier.getStaffphone(),courier.getPersonalid(),
                password,courier.getDelivery(),
                createtime,logintime);
    }

    public static List<BootstrapTableCourier> transferCourierList(List<Courier> courierList) {
        List<BootstrapTableCourier> btCourierList = new ArrayList<>();
        for (Courier courier: courierList) {
            btCourierList.add(transferCourier(courier));
        }
        return btCourierList;
    }

public static void setWechatCourier(HttpSession session, Courier courier) {
    session.setAttribute("wechatCourier", courier);
}

public static Courier getWechatCourier(HttpSession session) {
    Object obj = session.getAttribute("wechatCourier");
    if (obj!=null) {
        return (Courier) obj;
    } else {
        return null;
    }
}

}
